package com.bridgeintelligent.tag.webserver.dim.query.service.thread;

import com.alibaba.excel.ExcelWriter;
import com.bridgeintelligent.tag.webserver.dim.query.api.ThemeQueryModel;
import com.bridgeintelligent.tag.webserver.dim.query.service.QueryApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class ThemeThreadPoolExecutor {

    private Executor executor;

    public ThemeThreadPoolExecutor(@Qualifier("threadPool_theme") Executor executor) {
        this.executor = executor;
    }


    public void handler(Integer sheetNum, Integer ROW_SIZE, Integer oneSheetWriteCount, Integer lastSheetWriteCount, List<List<String>> head
            , ThemeQueryModel themeQueryModel, Integer WRITE_SIZE, QueryApi queryApi, ExcelWriter excelWriter) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(sheetNum);
        for (int i = 0; i < sheetNum; i++) {
            Thread.sleep(1000);
            ThemeWriteThread themeWriteThread = new ThemeWriteThread(countDownLatch,i,sheetNum,ROW_SIZE,oneSheetWriteCount,lastSheetWriteCount,head,themeQueryModel,WRITE_SIZE,queryApi,excelWriter);
            executor.execute(themeWriteThread);
        }
        try {
            long timeout = 3600L;
            countDownLatch.await(timeout, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            log.error("线程同步发生异常->{}", e.getMessage());
            Thread.currentThread().interrupt();
        }

    }
}
